﻿<%
response.reset();
response.setContentType("application/octet-stream");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-Disposition","attachment;filename=\"report.xls\"" ); 



%>

<%@ page language="java" contentType="text/html; charset=UTF-8"  
import="java.util.*,java.text.SimpleDateFormat,
com.brother.model.*,com.ibatis.common.util.PaginatedList
,com.brother.util.DateFormatter,com.brother.model.AlarmType,com.brother.util.Distance,com.brother.service.*,com.brother.util.MyPaginatedList"  pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ include file="international.jsp" %> 
<jsp:useBean id="trackerService" class="com.brother.service.TrackerService"></jsp:useBean>
<jsp:useBean id="report" class="com.brother.model.Report"></jsp:useBean>
<jsp:useBean id="reportService" class="com.brother.service.ReportService"></jsp:useBean>
<jsp:setProperty property="*" name="report"/>
<%
com.brother.model.Customer user = (com.brother.model.Customer)session.getAttribute(com.brother.Constants.LOGIN_SESSTION_NAME);
if(user == null){
	return;
}
String sessesionLocal = (String)session.getAttribute("locale");
System.out.println(" state:"+report.getState()+"trackerid:"+report.getTrackerid()+" param:"+request.getParameter("tracker.trackerid"));
List<Tracker> trackers = trackerService.findUserTracker(user.getUserid());

report.setUserId(user.getUserid());	
report.setOrderCondition("order by createtime asc");
String pageNo = request.getParameter("pageNo");
String action = "page";//request.getParameter("action");
if(pageNo == null || !pageNo.matches("^\\d*$")){
	pageNo = "0";
}

String dateFormat = (String)session.getAttribute(Constants.DATE_FORMAT_SESSION_NAME);
SimpleDateFormat formatEn = new SimpleDateFormat("MM/dd/yyyy"); // English date format


SimpleDateFormat formatCN = new SimpleDateFormat("yyyy-MM-dd"); // 


String extDateFormat = (String)session.getAttribute(Constants.EXT_DATE_FORMAT_SESSION_NAME);

if(action == null){
	report.setPageSize(50);
	Calendar calendar = Calendar.getInstance();
	calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) - 7);
	report.setCreateTimeFrom(formatCN.format(calendar.getTime()));
	report.setCreateTimeTo(formatCN.format(new Date())+" "+"23:59:59");
	report.setTrackerid("00000000");
	//session.setAttribute("searchRealTimeData",report);
} else if(action.equalsIgnoreCase("search")) {
	report.setCreateTimeTo(report.getCreateTimeTo()+" "+"23:59:59");
	//session.setAttribute("searchRealTimeData",report);
}else if(action.equalsIgnoreCase("page")){
	report = (Report)session.getAttribute("searchRealTimeData"); 
}
String backupTimeFrom = report.getCreateTimeFrom();
String backupTimeTo = report.getCreateTimeTo();

if(!DateFormatter.isChineaseFormat(report.getCreateTimeFrom())){
	report.setCreateTimeFrom(formatCN.format(formatEn.parse(report.getCreateTimeFrom())));
	report.setCreateTimeTo(formatCN.format(formatEn.parse(report.getCreateTimeTo())));
}

report.setUserId(user.getUserid());

DateFormatter format = new DateFormatter(); 
format.setConfig(user.getTimeZoneOffSet(), dateFormat+" HH:mm:ss");
report.setLanguage((String)session.getAttribute("locale")); 
PaginatedList pageData = reportService.findData(pageNo, 200000,report);
int totalSize = ((MyPaginatedList)pageData).getTotalSize();//reportService.findDataSize(report.getPageSize(),report);
int totalPages = totalSize / report.getPageSize() + 1;



report.setCreateTimeFrom(backupTimeFrom);
report.setCreateTimeTo(backupTimeTo);


String previousPageFirstDistance = request.getParameter("previousPageFirstDistance");
if(previousPageFirstDistance == null){
	previousPageFirstDistance = "0";
}
%>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title><fmt:message key="report.title"/> - <fmt:message key="company.name"/></title>
<meta name="description" content="<fmt:message key="description.keyword"/>" />
</head>
<body>

<table  width="100%" cellpadding="5" cellspacing="0">
		<thead>		
	<tr>
		<th width="150"><strong><fmt:message key="report.date"/>:</strong></th>

		<th width="80"><strong><fmt:message key="location.lat"/>:</strong></th>
		<th width="80"><strong><fmt:message key="location.lng"/>:</strong></th>
		<th width="60"><strong><fmt:message key="report.speed"/>:</strong></th>
		<th width="60"><strong><fmt:message key="report.alarm"/>:</strong></th>
		<th width="80"><strong><fmt:message key="report.distance"/>:</strong></th>
		<th width="250"><strong><fmt:message key="report.address"/>:</strong></th>
	</tr>
</thead>	



<%
java.text.DecimalFormat nf =new java.text.DecimalFormat("0.00");

Iterator<Report> datas = pageData.iterator();
float[] toPos = new float[2];
String lat = request.getParameter("lat");
String lng = request.getParameter("lng");

if(lat != null){
	try{
		toPos[0] = Float.parseFloat(lat);
		toPos[1] = Float.parseFloat(lng);
	}catch(Exception e){}
}
int row = 0;
LatLngPoint point = null;
double distance = 0;
while(datas.hasNext()){
	Report data = datas.next();
	if(row == 0 && pageNo.equals("0")){
		toPos[0] = data.getSignedLat();
		toPos[1] = data.getSignedLng();
	}
	if(point != null)
	distance = distance + Distance.distance(new LatLngPoint(data.getSignedLat(), data.getSignedLng()), point);
	point = new LatLngPoint(data.getSignedLat(), data.getSignedLng());
%>
		<tr style="cursor:default; color:<%if(data.getSignal().equalsIgnoreCase("ok")){%>#000000;<%}else{%>#A0A0A4;<%}%>" >
			<td style=" font-size:11px;"><b><%=format.format(data.getCreateTime()) %></b></td>
			<td style=" font-size:11px;"><%=data.getSignedLat()%></td>
			<td style=" font-size:11px;"><%=data.getSignedLng()%></td>
			<td style=" font-size:11px;"><%=data.getSpeed()%></td>
			<td style=" font-size:11px;"><%=AlarmTypeService.getAlarmType(data.getState(), sessesionLocal)%></td>
			<td style=" font-size:11px;"><%=nf.format(distance/1000)%>KM</td>
			<td style=" font-size:11px;" width="280px" ><%=data.getAddress() == null ? "" : data.getAddress()%></td>
		</tr>

<%
row +=1;

} %>	

		</table>



</body>
</html><%
com.brother.database.DataBase.closeThreadConnection();
%>